home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / indents.zip / args.c next >
C/C++ Source or Header  |  1993-05-30  |  15KB  |  542 lines

  1. /**
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that the above copyright notice and this paragraph are duplicated in all
  9.  * such forms and that any documentation, advertising materials, and other
  10.  * materials related to such distribution and use acknowledge that the
  11.  * software was developed by the University of California, Berkeley, the
  12.  * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  13.  * either University or Sun Microsystems may not be used to endorse or
  14.  * promote products derived from this software without specific prior written
  15.  * permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  17.  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include "globals.h"
  21.  
  22. #ifndef lint
  23. # ifndef ANSIC
  24. static char     sccsid[] = "@(#)args.c    6.0 (Berkeley) 92/06/15";
  25. # endif         /* ANSIC */
  26. #endif          /* not lint */
  27.  
  28. /* Argument scanning and profile reading code.  Default parameters are set
  29.  * here as well. */
  30.  
  31. #include <ctype.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34.  
  35. #ifdef ANSIC
  36. static void     scan_profile(FILE *);
  37. #endif          /* ANSIC */
  38.  
  39. /* Profile types */
  40. #define PRO_SPECIAL     1       /* special case */
  41. #define PRO_BOOL        2       /* Boolean */
  42. #define PRO_INT         3       /* integer */
  43. #define PRO_FONT        4       /* troff font */
  44.  
  45. /* Profile specials for Booleans. Booleans can be set to USE or IGNORE. USE
  46.  * reads the value, IGNORE ignores the value and keeps the default */
  47. #define USE             0       /* turn it off */
  48.  
  49. /* Profile specials for specials */
  50. #define IGNORE          1       /* ignore it */
  51. #define KEY             4       /* type (keyword) */
  52.  
  53. char           *option_source = "?";
  54.  
  55. /* N.B.: option names can't start with n */
  56. struct pro {
  57.     char           *p_name;     /* name, e.g. -br, -cli */
  58.     int             p_type;     /* type (int, bool, special) */
  59.     int             p_default;  /* the default value (if int) */
  60.     int             p_special;  /* depends on type */
  61.     int            *p_obj;      /* the associated variable */
  62. }               pro[] = {
  63.     {
  64.         "+", PRO_BOOL, true, USE, &cplus
  65.     },
  66.     {
  67.         "T", PRO_SPECIAL, 0, KEY, 0
  68.     },
  69.     {
  70.         "bacc", PRO_BOOL, false, USE, &blanklines_around_conditional_compilation
  71.     },
  72.     {
  73.         "bad", PRO_BOOL, false, USE, &blanklines_after_declarations
  74.     },
  75.     {
  76.         "badp", PRO_BOOL, false, USE, &blanklines_after_declarations_at_proctop
  77.     },
  78.     {
  79.         "bap", PRO_BOOL, false, USE, &blanklines_after_procs
  80.     },
  81.     {
  82.         "bbb", PRO_BOOL, false, USE, &blanklines_before_blockcomments
  83.     },
  84.     {
  85.         "bc", PRO_BOOL, false, USE, &ps.leave_comma
  86.     },
  87.     {
  88.         "br", PRO_BOOL, true, USE, &btype_2
  89.     },
  90.     {
  91.         "brr", PRO_BOOL, false, USE, &btype_3
  92.     },
  93.     {
  94.         "bs", PRO_BOOL, false, USE, &Bill_Shannon
  95.     },
  96.     {
  97.         "c", PRO_INT, 33, 0, &ps.com_ind
  98.     },
  99.     {
  100.         "cci", PRO_INT, 4, 0, &ps.case_code_indent
  101.     },
  102.     {
  103.         "cd", PRO_INT, 0, 0, &ps.decl_com_ind
  104.     },
  105.     {
  106.         "cdb", PRO_BOOL, false, USE, &comment_delimiter_on_blankline
  107.     },
  108.     {
  109.         "ce", PRO_BOOL, true, USE, &cuddle_else
  110.     },
  111.     {
  112.         "ci", PRO_INT, 0, 0, &continuation_indent
  113.     },
  114.     {
  115.         "cli", PRO_INT, 0, 0, &ps.case_indent
  116.     },
  117.     {
  118.         "cp", PRO_INT, 17, 0, &ps.else_endif_col
  119.     },
  120.     {
  121.         "d", PRO_INT, 0, 0, &ps.unindent_displace
  122.     },
  123.     {
  124.         "di", PRO_INT, 16, 0, &ps.decl_indent
  125.     },
  126.     {
  127.         "dj", PRO_BOOL, false, USE, &ps.ljust_decl
  128.     },
  129.     {
  130.         "eei", PRO_BOOL, false, USE, &extra_expression_indent
  131.     },
  132.     {
  133.         "ei", PRO_BOOL, true, USE, &ps.else_if
  134.     },
  135.     {
  136.         "fb", PRO_FONT, 0, 0, (int *) &bodyf
  137.     },
  138.     {
  139.         "fbc", PRO_FONT, 0, 0, (int *) &blkcomf
  140.     },
  141.     {
  142.         "fbx", PRO_FONT, 0, 0, (int *) &boxcomf
  143.     },
  144.     {
  145.         "fc", PRO_FONT, 0, 0, (int *) &scomf
  146.     },
  147.     {
  148.         "fc1", PRO_BOOL, false, USE, &format_col1_comments
  149.     },
  150.     {
  151.         "fk", PRO_FONT, 0, 0, (int *) &keywordf
  152.     },
  153.     {
  154.         "fs", PRO_FONT, 0, 0, (int *) &stringf
  155.     },
  156.     {
  157.         "i", PRO_INT, 4, 0, &ps.ind_size
  158.     },
  159.     {
  160.         "ip", PRO_BOOL, true, USE, &ps.indent_parameters
  161.     },
  162.     {
  163.         "l", PRO_INT, 78, 0, &max_col
  164.     },
  165.     {
  166.         "lc", PRO_INT, 0, 0, &block_comment_max_col
  167.     },
  168.     {
  169.         "ldefs", PRO_BOOL, false, USE, &list_defines
  170.     },
  171.     {
  172.         "lp", PRO_BOOL, true, USE, &lineup_to_parens
  173.     },
  174.     {
  175.         "pcs", PRO_BOOL, false, USE, &proc_calls_space
  176.     },
  177.     {
  178.         "pro", PRO_BOOL, true, USE, &useProfile
  179.     },
  180.     {
  181.         "prs", PRO_BOOL, false, USE, &parens_space
  182.     },
  183.     {
  184.         "ps", PRO_BOOL, false, USE, &pointer_as_binop
  185.     },
  186.     {
  187.         "psl", PRO_BOOL, false, USE, &procnames_start_line
  188.     },
  189.     {
  190.         "sc", PRO_BOOL, true, USE, &star_comment_cont
  191.     },
  192.     {
  193.         "sob", PRO_BOOL, false, USE, &swallow_optional_blanklines
  194.     },
  195.     {
  196.         "st", PRO_BOOL, false, USE, &useStdio
  197.     },
  198.     {
  199.         "tabs", PRO_INT, 8, 0, &tabsize
  200.     },
  201.     {
  202.         "tabu", PRO_BOOL, false, 0, &usetabs
  203.     },
  204.     {
  205.         "troff", PRO_BOOL, false, USE, &troff
  206.     },
  207.     {
  208.         "v", PRO_BOOL, false, USE, &verbose
  209.     },
  210.     {
  211.         0, 0, 0, 0, 0
  212.     },
  213.     /* whew! */
  214. };
  215.  
  216. /**
  217.  * NAME: void set_profile(void)
  218.  *
  219.  * FUNCTION: set_profile reads $HOME/indent.pro and ./indent.pro and handles
  220.  * arguments given in these files.
  221.  *
  222.  * ALGORITHM:
  223.  *
  224.  * PARAMETERS: Reads the environment.
  225.  *
  226.  * RETURNS:
  227.  *
  228.  * GLOBALS READ:
  229.  *
  230.  * GLOBALS CHANGED:
  231.  *
  232.  * CALLED BY:
  233.  *
  234.  * HISTORY: Peter Hadfield 09-03-93 Removed the '.' (UNIX hidden file marker)
  235.  * from indent.pro to allow the same code to be used for DOS and UNIX.
  236.  *
  237.  * Added display of profile file path for verbose option.
  238.  * Fixed problem with (null) being written into fname if getenv() returns NULL.
  239.  *
  240.  * Added reading of a profile file with the same path and name as the binary
  241.  * (but with a .pro extension).*/
  242.  
  243. #ifdef ANSIC
  244. void            set_profile(char *bin_name)
  245. #else           /* ANSIC */
  246. set_profile(bin_name)
  247.     char           *bin_name;
  248. #endif          /* ANSIC */
  249. {
  250.     FILE           *stream;
  251.     char            fname[BUFSIZ] = "";
  252.     char           *env_ptr = NULL;
  253.     static char     prof[] = "indent.pro";  /* this is no longer a UNIX
  254.                                              * hidden file so the DOS port
  255.                                              * can use the same code */
  256.     strcpy(fname, bin_name);
  257.     if (fname[strlen(fname) - 4] == '.')    /* assume its a DOS file name */
  258.         (fname[strlen(fname) - 4] = '\0');  /* remove the extension */
  259.     strcat(fname, ".pro");
  260.     if ((stream = fopen(option_source = fname, "r")) != NULL) {
  261.         if ((verbose) || (show_options))
  262.             fprintf(stderr, "Reading profile file: %s", fname);
  263.         scan_profile(stream);
  264.         (void) fclose(stream);
  265.     }
  266.     env_ptr = getenv("HOME");
  267.     if (env_ptr != NULL) {
  268.         sprintf(fname, "%s/%s", env_ptr, prof);
  269.         if ((stream = fopen(option_source = fname, "r")) != NULL) {
  270.             if ((verbose) || (show_options))
  271.                 fprintf(stderr, "\nReading profile file: %s", fname);
  272.             scan_profile(stream);
  273.             (void) fclose(stream);
  274.         }
  275.     }
  276.     sprintf(fname, "%s", prof);
  277.     if ((stream = fopen(option_source = fname, "r")) != NULL) {
  278.         if ((verbose) || (show_options))
  279.             fprintf(stderr, "Reading profile file: %s", fname);
  280.         scan_profile(stream);
  281.         (void) fclose(stream);
  282.     }
  283.     option_source